﻿/*****************************************************************************
* File			: src.Missile
* Purpose		: Contains Declaration and Implementation for Missile class
* Creation date : April 24, 2009
* Author		: Sami TOUIL
******************************************************************************/
package src 
{
	// Imports
	import flash.display.MovieClip;
	import flash.events.*
	
	/**************************************
	* Class		: Missile
	* Purpose	: Logic for missiles behavior
	* Parent	: MovieClip
	**************************************/
	public class Missile extends MovieClip
	{
		// Class variables
		private var nSpeed		:int = 10;
		private var nRotation	:int;
	
		// Constructor
		public function Missile(nX, nY, nRotationParam)
		{
			// Set listener for ENTER_FRAME event
			addEventListener(Event.ENTER_FRAME, enterFrame);
			
			// Set coords
			x = nX;
			y = nY;
			
			// Set rotation
			nRotation = nRotationParam;
			
			// Add Missile sprite
			Game.main.spriteClip.addChild(this);
		}
		
		/***********************************************************
		* Method		: enterFrame
		* Purpose		: frame loop
		* Parameters	: e:Event
		* return		: void
		***********************************************************/
		public function enterFrame(e:Event)
		{
			// Clear if the hero ship is dead
			if (Game.main.ship == null)
			{
				onDelete();
				return;
			}
			else
			{
				// Missile movement
				x += nSpeed;
				
				// Missile rotation
				rotation += nRotation;
				
				// Delete missile instance once it's not in the frame anymore
				if(x > Game.WIDTH )
				{
					onDelete();
					return;
				}
					
				// Collision management with enemies
				for(var currentEnemy in Enemy.enemiesList)
				{
					if(this.hitTestObject(Enemy.enemiesList[currentEnemy]))
					{
						// Delete missile instance, and trigger takeDamage on enemy
						Enemy.enemiesList[currentEnemy].takeDamage();
						onDelete();
						return;
					}
				}
			}
		}
		
		/***********************************************************
		* Method		: onDelete
		* Purpose		: called for deleting the object instance
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function onDelete()
		{
			removeEventListener(Event.ENTER_FRAME, enterFrame);
			Game.main.spriteClip.removeChild(this);
		}
	}
}